#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QMainWindow>

#include "./db/dbconnection.h"
#include "./db/dbfabric.h"

#include "./gl/glwidget.h"
#include "gl/glmapwidget.h"
#include "gl/glemfwidget.h"

#include "./settings/settings.h"
#include "./treeview/treeviewadapter.h"

#include "./drawables/picketdrawable.h"
#include "./drawables/circleloopdrawable.h"
#include "./drawables/squareloopdrawable.h"
#include "./drawables/emfdrawable.h"
#include "./drawables/builders/drawablebuilder.h"
#include "./drawables/builders/picketdrawablebuilder.h"
#include "./drawables/builders/loopdrawablebuilder.h"
#include "./drawables/builders/emfdrawablebuilder.h"

#include "./infodocks/builders/infodockelementsbuilder.h"
#include "./infodocks/builders/picketinfodockbuilder.h"
#include "./infodocks/builders/loopinfodockbuilder.h"

#include "./polygondata/polygondata.h"
#include "./tagable/tagable.h"

#include "dialogs/polygondialog.h"
#include "dialogs/configurationdialog.h"

#include<QtGui>
#include<QMessageBox>
#include<QSqlError>
#include<QWidget>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    GLWidget *glWidget;

    QVBoxLayout *rightPanel;
    TreeViewAdapter *adapter;
    QSharedPointer<Settings> settings;
    QSharedPointer<DbConnection> dbconnect;
    QSharedPointer<PolygonData> polygonData;

    PolygonDialog *polygonDialog;
    ConfigurationDialog *configDialog;

    QLineEdit *timeEdit;

    QString _idPolygon;
private:
    enum displayMode
    {
        EMF,
        MAP
    };

    displayMode _mode;

    Ui::MainWindow *ui;

    void _initEventHandlers();
    void _initDbConnection();
    void _initGlWidget();
    void _initSettings();
    void _initRightPanel();
    void _initLeftPanel();
    void _initToolBarMap();

    void _initDataViews();

    void _addDrawables(ElementIterator *iterator);
    void _addInfodocks(QSharedPointer<QMap<int, QPointF> > emfMap);

    void _clearLayout(QLayout *layout);
    int _getTreeSelectedItem(QModelIndex index);

    void _showDrawables(int selectedId);
    void _showInfoDocks(int selectedId);

    bool _initDbConfiguration();

    void _initPolygonDialog();
    void _loadData(QString idPolygon);

    void _switchGlWidgetToEmf();
    void _switchGlWidgetToMap();

    void _setIcon(QString filepath);


public slots:
    void onTreeViewDoubleClick(QModelIndex index);
    void onGlWidgetClick(int id);
    void contextMenu(const QPoint & point);

    void onMenuConfigConnectionActivate();
    void onMenuDatasetSelectActivate();
    void onMenuReconnectActivate();

    void onEmfSelected(QSharedPointer<QMap<int, QPointF> > emfMap);

    void onIncreaseXScale();
    void onDecreaseXScale();
    void onIncreaseYScale();
    void onDecreaseYScale();
    void onOneToOne();

    void onDecorate();
    void onAverage();
};

#endif // MAINWINDOW_H
